//
//  YHComposePictureView.swift
//  YHWeibo
//
//  Created by yuhao.mac on 16/7/3.
//  Copyright © 2016年 yuhao.mac. All rights reserved.
//

import UIKit

private let cellReuseId = "cellReuseId"

class YHComposePictureView: UICollectionView {
    
    //通知主控制器弹出对应控制器
    var composePictureViewClosure:(()->())?
    
    //创建数组
    var imageList:[UIImage] = [UIImage]()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //供外界调用的方法
    func addImages(image:UIImage){
        imageList.insert(image, atIndex: 0)
        if imageList.count > 9 {
            imageList.removeLast()
        }
        hidden = false
        
        reloadData()
        
    }
    
    private func setupUI(){
        //首次进入的时候把配图隐藏
        hidden = true
        
        backgroundColor = UIColor.whiteColor()
        
        registerClass(YHComposePictureCell.self, forCellWithReuseIdentifier: cellReuseId )
        
        dataSource = self
        delegate = self
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let itemMargin:CGFloat = 5
        let cellWH = (SCREENW - 20 - itemMargin*2)/3
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
    }

}

extension YHComposePictureView:UICollectionViewDataSource,UICollectionViewDelegate{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = imageList.count
        
        if count == 0 || count == 9{
            return count
        }else{
            return count + 1
        }

    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellReuseId , forIndexPath: indexPath) as! YHComposePictureCell
        
        if imageList.count == indexPath.item{
            
            cell.image = nil
        }else{
            cell.image = imageList[indexPath.item]
        }
        
        cell.deleteBtnClosure = {
            
            //删除指定元素
            self.imageList.removeAtIndex(indexPath.item)
            
            if self.imageList.count == 0{
                self.hidden = true
            }
            //刷新
            self.reloadData()
        }
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        composePictureViewClosure?()
    }
}

class YHComposePictureCell: UICollectionViewCell {
    
    var deleteBtnClosure:(()->())?
    
    //供外界调用
    var image:UIImage?{
        didSet{
            if image == nil{
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                //隐藏删除按钮
                deleteButton.hidden = true
                
            }else {
                //正常显示
                bgImageView.image = image
                //解决cell重用问题
                bgImageView.highlightedImage = nil
                
                deleteButton.hidden = false
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.right.top.equalTo(contentView)
        }
        
    }
    
    private lazy var bgImageView:UIImageView = UIImageView()
    
    private lazy var deleteButton:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    @objc private func deleteButtonClick(){
        
        deleteBtnClosure?()
    }
}

















